(function(){
	this.Interpreter = {
		_LANGUAGE : "text/ECMAScript6",
		_codeBlock : [],
	    _error : function (errorNum){
			switch(errorNum) {
				case 0:
					throw new Error("Missing keyword 'Class'");
					break; 
				case 1:
					throw new Error("Unexpected token Class");
					break; 
				default:
		  }
	    },
		init : function(){
			var sTag = document.getElementsByTagName("script");
			for (var i = 0; i < sTag.length; i++) {
				var tType = sTag[i].getAttribute("type");
				if (tType==this._LANGUAGE) {
					this._codeBlock.push(sTag[i].innerText); 
				}
			} 
			String.prototype.trim = function(){
			return this.replace(/(^\s*)|(\s*$)/g, "");
			}
			String.prototype.ltrim = function(){
			return this.replace(/(^\s*)/g,"");
			}
			String.prototype.rtrim = function(){
			return this.replace(/(\s*$)/g,"");
			}
			String.prototype.wrap = function(){
			return this.replace(/[\r\n]/g,"");
			}
			this._explain(); //开始解析代码
		},
		docReady : function(_fn){
			window.tInterval = setInterval(function(){
				if (document.readyState=="complete"){
					console.log(document.readyState);
					window.clearInterval(window.tInterval);
					_fn.call();
				}
			},1);
		},
		_getAllFuNames : function(ecmasSriptCode){
			 var newCode = ecmasSriptCode; 
			 var tFuncName = null;
			 var funcNames = [];
			 var i = -1;
			 do {
			 	i = i + 1;
			    var tFuncName = newCode.substring(newCode.indexOf("}") + 1,newCode.indexOf("()"));
			    funcNames.push(tFuncName);
			    var newCode = newCode.replace("}" + tFuncName + "(",""); 
			 }
			 while (i <= (newCode.split('{').length-5));
			 return funcNames;
		},
		_syntaxCheck : function(ecmasSriptCode){
			var tSyntactic = ecmasSriptCode.split("Class "); 
			if ( tSyntactic.length == 1) { //Class关键字没写
			    this._error(0);return false;
			}
			if ( tSyntactic[0] != "\n" ) {
				this._error(1);return false; //Class关键字前面有非法字符
			}
			return true;
		},
		_explain : function() {
			for (var i = 0; i < this._codeBlock.length; i++) {
				this._esToJs(this._codeBlock[i]);
			}
		},
		_esToJs : function(ecmasSriptCode) {  
			if ( this._syntaxCheck(ecmasSriptCode) ){ 
				var ecmasSriptCode = ecmasSriptCode.trim().wrap(); 
				var ttSyntactic = ecmasSriptCode.split("Class "); 
				var jsCode = [];
				for (var i = 1; i < ttSyntactic.length; i++) {
					 var className = ttSyntactic[i].substring(0,ttSyntactic[i].indexOf("{")).trim();
					 var faNumber = ttSyntactic[i].lastIndexOf("{");
					 var laNumber = ttSyntactic[i].lastIndexOf("}")+2; 
					 var newCode = ttSyntactic[i].replace(className, ""); 
					 newCode = ("Class.$(\""+ className + "\"," + newCode.substring(0,laNumber).replace(/\s/g, "")); 
					 newCode = newCode.replace("constructor" , "_constructor" + " : function");
					 var funcNames = this._getAllFuNames(newCode);
					 for (var b = 0; b < funcNames.length; b++) {
					 	newCode = newCode.replace(funcNames[b], funcNames[b] + " : function");
					 }
					 newCode = newCode.replace(/\}/g,"},"); 
					 newCode = newCode.replace(/return/g,"return "); 
					 newCode = newCode.replace(/var/g,"var ");
					 newCode = newCode.substring(0,newCode.length-1)+");"; 
					 jsCode.push(newCode);  
				}  
				this._execute(jsCode);
			}
		},
		_execute : function(jsCode) { 
	 		for (var i = 0; i < jsCode.length; i++) { 
	 			new Function(jsCode[i])();
	 		}
		},
	}
})(this);